<template>
	<!-- 产品列表 -->
	<view class="card" v-for="(item,index) in productsData.data" :key="index" @click="GoUrl('../shop_detail/shop_detail?pid='+(props.isFav?item.productInfo.id:item.id))">
		<!-- border-2 border-black-500  -->
		<view class="flex justify-center items-center py-2 title">
			<view class="flex-[0.88] text-center box-border pl-12 blod text-lg word">{{props.isFav?item.productInfo.maintype:item.maintype}}系列</view>
			<view class="flex flex-[0.22] justify-center items-center box-border more text-sm">
					<view class="text-gray-400" @click.stop="GoUrl('../class/class',2)">更多</view>
					<view><u-icon name="arrow-right" color="#999" size="15"></u-icon></view>
				</view>
		</view>
		<!-- https://zzwj.cc/web/uploads/mall1/20210630/c9e56969ebd959f0cf61d7da465b77306c265122.jpg -->
		<view class="w-full h-auto p-2 flex justify-between items-center details">
			<view class="w-[250rpx] h-auto flex flex-[0.35] justify-center items-center imgs">
				<u--image width="250rpx" height="300rpx"  :src="props.isFav?item.productInfo.productimg:item.productimg" shape="square" :radius="20" mode="aspectFit"></u--image>
			</view>
			<view class="w-full h-[220rpx] flex flex-[0.63] flex-col justify-start items-start bodys">
				<view class="w-full h-[120rpx] overflow-hidden desc">{{limtStrLen(props.isFav?item.productInfo.productdesc:item.productdesc,25)}}</view>
				<view class="w-full h-auto overflow-hidden price text-red-500">￥{{props.isFav?(item.productInfo.productdiscountprice!=""?item.productInfo.productdiscountprice:item.productInfo.productprice):(item.productdiscountprice!=""?item.productdiscountprice:item.productprice)}}元</view>
				<view class="w-full h-auto overflow-hidden mt-2 flex justify-between">
					<view class="text-gray-400 text-sm price">已售{{props.isFav?item.productInfo.productselled:item.productselled}}</view>
					<view v-show="props.isFav" class="text-gray-400 text-sm price" @click.stop="deleteData(item.id,index)">移除</view>
				</view>
			</view>
		</view>
	</view> 
</template>

<script setup lang='ts'>
import { ref,reactive,onMounted } from 'vue'
import { GoUrl,limtStrLen } from '@/utils/utils'
import { getUserFavourites,deleteFavouritesData, getAllProducts } from '@/utils/apiFunction'
import { userStore } from '@/stores/userStore'	
const userstore = userStore();

//当前登录的用户id
let userid=ref(0);
let productsData=reactive({data:[]})
		
const props=defineProps({
	//是否收藏模式
	isFav:{
		type:Boolean,
		default:false
	}
})

//删除收藏数据
const deleteData=(favId:number,index:number)=>{
	//console.log("删除id",favId);
	uni.showModal({
		title: '操作提示',
		content: '是否移除该收藏',
		showCancel: true,
		success:async res => {
			if(res.confirm){
				console.log("删除了数据"+favId);
				let rct:any=await deleteFavouritesData(favId);
				if(rct.data.code===200){
					productsData.data.splice(index,1);
				}
			}
		},
		fail: () => {},
		complete: () => {}
	});
}

onMounted(async ()=>{
	//console.log("产品组件加载了")	
	
	//我的收藏-浏览痕迹模式
	if(props.isFav){
		userid.value=userstore.userData.userinfo.id;
		console.log("userid.value:",userid.value);
		if(userid.value===undefined || userid.value===null || userid.value===0){
			GoUrl('../mine/mine',2);
			return;
		}
		if(userid.value!=null && userid.value!=''){
			getUserFavourites(userid.value).then(res=>{
				console.log("获取用户收藏",res);
				if(res.data.code===200){
					productsData.data=res.data.data;
				}
			})
		}		
	}else{
		getAllProducts().then(res=>{
			console.log("首页获取产品信息",res);
			if(res.data.code===200){
				productsData.data=res.data.data;
			}
		})
	}

});
		
</script>

<style lang='scss' scoped>
	@import "Aproduct.scss";
</style>